package com.ifnk.portaljava.dapWeb.socket;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.util.Date;

public class SocketProperty {
	public Socket socket;
	private String address;
	private int port;
	public InputStream in;
	public OutputStream out;
	private String clientId;
	public Boolean isNetConnected;
	private String createTime;
	private String error;
	public Boolean isLaunchHeartcheck;
	public Boolean flag = true;
	public Log loger = LogFactory.getLog(SocketProperty.class);

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public InputStream getIn() {
		return in;
	}

	public void setIn(InputStream in) {
		this.in = in;
	}

	public OutputStream getOut() {
		return out;
	}

	public void setOut(OutputStream out) {
		this.out = out;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public Boolean getIsNetConnected() {
		if (isNetConnected == null) {
			isNetConnected = false;
		}
		return isNetConnected;
	}

	public void setIsNetConnected(Boolean isNetConnected) {
		this.isNetConnected = isNetConnected;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public Boolean getIsLaunchHeartcheck() {
		return isLaunchHeartcheck;
	}

	public void setIsLaunchHeartcheck(Boolean isLaunchHeartcheck) {
		this.isLaunchHeartcheck = isLaunchHeartcheck;
	}

	public synchronized Boolean getFlag() {
		return flag;
	}

	public synchronized void setFlag(Boolean flag) {
		this.flag = flag;
	}

	public void createSocket() {
		Socket socket;
		try {
			socket = new Socket(address, port);
			this.setSocket(socket);
			this.setOut(socket.getOutputStream());
			this.setIn(socket.getInputStream());
			this.setIsNetConnected(true);
			Date date = new Date();
			DateFormat dateformat = DateFormat.getDateTimeInstance();
			this.setCreateTime(dateformat.format(date));
		} catch (UnknownHostException e) {
			this.setIsNetConnected(false);
			this.setError("UnknownHostException");
			e.printStackTrace();
			loger.error("dapWeb Exception " + e.toString());
		} catch (IOException e) {
			this.setIsNetConnected(false);
			this.setError("IOException");
			e.printStackTrace();
			loger.error("dapWeb Exception " + e.toString());
		}

	}

	public void createSocket(String address, int port) {
		Socket socket;
		this.address = address;
		this.port = port;
		try {
			socket = new Socket(address, port);
			this.setSocket(socket);
			this.setOut(socket.getOutputStream());
			this.setIn(socket.getInputStream());
			this.setIsNetConnected(true);
			Date date = new Date();
			DateFormat dateformat = DateFormat.getDateTimeInstance();
			this.setCreateTime(dateformat.format(date));
			loger.info("dapWeb connect webproxy success! clientId:" + this.clientId + " sp.flag:" + this.flag);
		} catch (UnknownHostException e) {
			this.setIsNetConnected(false);
			this.setError("UnknownHostException");
			e.printStackTrace();
			loger.error("dapWeb Exception " + e.toString());
		} catch (IOException e) {
			this.setIsNetConnected(false);
			this.setError("IOException");
			e.printStackTrace();
			loger.error("dapWeb Exception " + e.toString());
		}

	}

	public void destroySocket() {
		try {
			in.close();
			out.close();
			socket.close();
			this.setIsNetConnected(false);

		} catch (IOException e) {
			e.printStackTrace();
			loger.error("dapWeb Exception " + e.toString());
		}

	}

}
